#!/bin/bash

BUILD_THIRD_PARTY_PROTOBUF_BUILD_DIR="@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_BUILD_DIR@" ;
BUILD_THIRD_PARTY_PROTOBUF_HOST_BUILD_DIR="@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_BUILD_DIR@" ;
BUILD_THIRD_IS_CROSS="@CMAKE_CROSSCOMPILING@"
BUILD_THIRD_IS_CROSS="$(echo $BUILD_THIRD_IS_CROSS | tr '[:upper:]' '[:lower:]')"
LOW_MEMORY_MODE="@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_LOW_MEMORY_MODE@"
LOW_MEMORY_MODE="$(echo $LOW_MEMORY_MODE | tr '[:upper:]' '[:lower:]')"

if [[ "x$LOW_MEMORY_MODE" == "xtrue" ]] || [[ "x$LOW_MEMORY_MODE" == "xyes" ]] || 
    [[ "x$LOW_MEMORY_MODE" == "x1" ]] || [[ "x$LOW_MEMORY_MODE" == "xon" ]]; then
    ALLOW_PARALLEL_BUILD_FLAGS=0 ;
else
    ALLOW_PARALLEL_BUILD_FLAGS=1 ;
fi

set -x ;

# build host protoc first
if [[ "x$BUILD_THIRD_IS_CROSS" == "xtrue" ]] || [[ "x$BUILD_THIRD_IS_CROSS" == "xyes" ]] || [[ "x$BUILD_THIRD_IS_CROSS" == "x1" ]] || [[ "x$BUILD_THIRD_IS_CROSS" == "xon" ]]; then
    export PATH="$BUILD_THIRD_PARTY_PROTOBUF_HOST_BUILD_DIR:$PATH" ;

    if [[ ! -e "$BUILD_THIRD_PARTY_PROTOBUF_HOST_BUILD_DIR" ]]; then
        mkdir -p "$BUILD_THIRD_PARTY_PROTOBUF_HOST_BUILD_DIR" ;    
    fi

    cd "$BUILD_THIRD_PARTY_PROTOBUF_HOST_BUILD_DIR" ;

    if [[ -e "CMakeCache.txt" ]]; then
        rm -f "CMakeCache.txt" ;
    fi

    if [[ ! -e "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/bin/protoc" ]] && [[ ! -e "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/bin/protoc.exe" ]]; then

        env CC= CXX= SDKROOT= @ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_BUILD_FLAGS_CMD@ "-DCMAKE_INSTALL_PREFIX=@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@" "-DCMAKE_POLICY_DEFAULT_CMP0091=NEW" ;

        if [[ "x@MSVC@" == "xTrue" ]] || [[ "x@MSVC@" == "xYes" ]] || [[ "x@MSVC@" == "x1" ]]; then

            if [[ $ALLOW_PARALLEL_BUILD_FLAGS -eq 1 ]]; then
                "@CMAKE_COMMAND@" --build . --target install --config Debug -j || "@CMAKE_COMMAND@" --build . --target install --config Debug ;
                "@CMAKE_COMMAND@" --build . --target install --config Release -j || "@CMAKE_COMMAND@" --build . --target install --config Release ;
            else
                "@CMAKE_COMMAND@" --build . --target install --config Debug ;
                "@CMAKE_COMMAND@" --build . --target install --config Release ;
            fi

            if [[ "x@CMAKE_BUILD_TYPE@" != "x" ]] && [[ "x@CMAKE_BUILD_TYPE@" != "xRelease" ]]; then
                if [[ $ALLOW_PARALLEL_BUILD_FLAGS -eq 1 ]]; then
                    "@CMAKE_COMMAND@" --build . --target install --config "@CMAKE_BUILD_TYPE@" -j || "@CMAKE_COMMAND@" --build . --target install --config "@CMAKE_BUILD_TYPE@" ;
                else
                    "@CMAKE_COMMAND@" --build . --target install --config "@CMAKE_BUILD_TYPE@" ;
                fi
            fi

        else

            if [[ $ALLOW_PARALLEL_BUILD_FLAGS -eq 1 ]]; then
                "@CMAKE_COMMAND@" --build . --target install --config Release -j || "@CMAKE_COMMAND@" --build . --target install --config Release ;
            else
                "@CMAKE_COMMAND@" --build . --target install --config Release ;
            fi

        fi

    fi

    export PATH="@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/bin:$PATH" ;

else
    export PATH="$BUILD_THIRD_PARTY_PROTOBUF_BUILD_DIR:$PATH" ;
fi

if [[ ! -e "$BUILD_THIRD_PARTY_PROTOBUF_BUILD_DIR" ]]; then
    mkdir -p "$BUILD_THIRD_PARTY_PROTOBUF_BUILD_DIR" ;    
fi

cd "$BUILD_THIRD_PARTY_PROTOBUF_BUILD_DIR" ;

if [[ -e "CMakeCache.txt" ]]; then
    rm -f "CMakeCache.txt" ;
fi

if [[ "x$BUILD_THIRD_IS_CROSS" == "xtrue" ]] || [[ "x$BUILD_THIRD_IS_CROSS" == "xyes" ]] || [[ "x$BUILD_THIRD_IS_CROSS" == "x1" ]] || [[ "x$BUILD_THIRD_IS_CROSS" == "xon" ]]; then

    @ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_BUILD_FLAGS_CMD@ "-DCMAKE_INSTALL_PREFIX=@PROJECT_THIRD_PARTY_INSTALL_DIR@" "-DCMAKE_POLICY_DEFAULT_CMP0091=NEW" -Dprotobuf_BUILD_LIBPROTOC=ON -Dprotobuf_BUILD_PROTOC_BINARIES=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF ;

else

    @ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_BUILD_FLAGS_CMD@ "-DCMAKE_INSTALL_PREFIX=@PROJECT_THIRD_PARTY_INSTALL_DIR@" "-DCMAKE_POLICY_DEFAULT_CMP0091=NEW" ;

fi

if [[ "x@MSVC@" == "xTrue" ]] || [[ "x@MSVC@" == "xYes" ]] || [[ "x@MSVC@" == "x1" ]]; then

    if [[ $ALLOW_PARALLEL_BUILD_FLAGS -eq 1 ]]; then
        "@CMAKE_COMMAND@" --build . --target install --config Debug -j || "@CMAKE_COMMAND@" --build . --target install --config Debug ;
        "@CMAKE_COMMAND@" --build . --target install --config Release -j || "@CMAKE_COMMAND@" --build . --target install --config Release ;
    else
        "@CMAKE_COMMAND@" --build . --target install --config Debug ;
        "@CMAKE_COMMAND@" --build . --target install --config Release ;
    fi

    if [[ "x@CMAKE_BUILD_TYPE@" != "x" ]] && [[ "x@CMAKE_BUILD_TYPE@" != "xRelease" ]]; then
        if [[ $ALLOW_PARALLEL_BUILD_FLAGS -eq 1 ]]; then
            "@CMAKE_COMMAND@" --build . --target install --config "@CMAKE_BUILD_TYPE@" -j || "@CMAKE_COMMAND@" --build . --target install --config "@CMAKE_BUILD_TYPE@" ;
        else
            "@CMAKE_COMMAND@" --build . --target install --config "@CMAKE_BUILD_TYPE@" ;
        fi
    fi

else

    if [[ $ALLOW_PARALLEL_BUILD_FLAGS -eq 1 ]]; then
        "@CMAKE_COMMAND@" --build . --target install --config Release -j || "@CMAKE_COMMAND@" --build . --target install --config Release ;
    else
        "@CMAKE_COMMAND@" --build . --target install --config Release ;
    fi

fi

if [[ $? -eq 0 ]] && [[ -e "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_REPOSITORY_DIR@/python/setup.py" ]]; then
    export PATH="@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/bin:$PATH" ;
    if [[ -e "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/python" ]]; then
        rm -rf "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/python";
    fi
    cp -rf "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_REPOSITORY_DIR@/python" "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/python";
    cp -rf "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/include" "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/src";
    cd "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/python";
    
    PYTHON_BIN="";
    if [[ "x" != "x@PYTHON_EXECUTABLE@" ]]; then
        "@PYTHON_EXECUTABLE@" --version > /dev/null 2>&1;
        if [[ $? -eq 0 ]]; then
            PYTHON_BIN="@PYTHON_EXECUTABLE@";
        fi
    fi
    if [[ "x" == "x$PYTHON_BIN" ]]; then
        python3 --version > /dev/null 2>&1;
        if [[ $? -eq 0 ]]; then
            PYTHON_BIN="python3";
        fi
    fi
    if [[ "x" == "x$PYTHON_BIN" ]]; then
        python --version > /dev/null 2>&1;
        if [[ $? -eq 0 ]]; then
            PYTHON_BIN="python";
        fi
    fi
    if [[ "x" != "x$PYTHON_BIN" ]]; then
        "$PYTHON_BIN" ./setup.py build_py ;
    fi
    rm -rf "@ATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_PROTOBUF_HOST_ROOT_DIR@/src";
fi
